package com.china08.yxyapi.api.manage.sch.office.meetingv1_1;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.meeting.VideoMeetingInviteUser;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.VideoMeetingInviteUserRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 特邀人员添加
 * 
 */
@RestController
public class VideoMeetingv1_1InviteUserSaveController extends YxyApiBaseController {

    @Autowired
    private VideoMeetingInviteUserRepository videoMeetingInviteUserRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/meetingv1_1/inviteuser/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        reqModel.check();
        RespModel model = new RespModel();

        YxyProfile yxyProfile = getProfile(request, response);
        String schooId = yxyProfile.getSchoolId();

        List<VideoMeetingInviteUser> inviteUserLst = videoMeetingInviteUserRepository
                .findByMeetingIdAndSchoolId(reqModel.meetingId, schooId);
        videoMeetingInviteUserRepository.delete(inviteUserLst);

        inviteUserLst = new ArrayList<VideoMeetingInviteUser>();

        for (InviteUser user : reqModel.inviteUserLst) {
            VideoMeetingInviteUser inviteUser = new VideoMeetingInviteUser();
            inviteUser.setMeetingId(reqModel.meetingId);
            inviteUser.setSchoolId(schooId);
            inviteUser.setUserId(user.userId);
            inviteUser.setUserNick(user.nick);
            inviteUser.setCreatedBy(yxyProfile.getId());
            inviteUserLst.add(inviteUser);
        }
        videoMeetingInviteUserRepository.save(inviteUserLst);

        model.setMessage("发起会议成功");
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 会议id
        public String meetingId;
        // 人员列表
        public List<InviteUser> inviteUserLst;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(meetingId, null), "会议编号不能为空");
        }
    }

    protected static class InviteUser {
        // 用户id
        public String userId;
        // 用户昵称
        public String nick;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {

    }
}
